package iotmonitor.system; //修改此处，你的包名称

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;

public class FlowerList {// 定义鲜花列表
    int maxLength = 6;
    int length = 0;
    Flower[] data = new Flower[maxLength];// 存入鲜花数据

    Scanner input = new Scanner(System.in);


    public FlowerList() {

    }

    // 添加记录方法
    public void add() {
        if (length < maxLength) {
            data[length] = new Flower();
            System.out.print("请输入花名： ");
            data[length].setName(input.next());
            System.out.print("请输入花的价格：");
            data[length].setPrice(input.nextDouble());
            System.out.print("请输入花的数量：");
            data[length].setAmount(input.nextInt());
            length++;
            System.out.println("添加成功！");
        } else {
            System.out.println("添加失败！");
        }
    }

    public void writeToFile(String fileName) {
        try (BufferedWriter writer = new BufferedWriter(new FileWriter(fileName))) {
            for (int i = 0; i < length; i++) {
                writer.write(data[i].getName() + "\t" + data[i].getPrice() + "\t" + data[i].getAmount());
                writer.newLine();
            }
            System.out.println("数据已成功写入文件：" + fileName);
        } catch (IOException e) {
            System.out.println("写入文件时发生错误：" + e.getMessage());
        }
    }
    // 显示花名方法
    public void transcript() {
        System.out.println("=====================================");
        for (int i = 0; i < length; i++) {
            System.out.println(data[i].getName() + "\t" + data[i].getPrice() + "\t" + data[i].getAmount());
        }
        System.out.println("=====================================\n");
    }
    public void sort() {// 按价格从高往低排序
        Flower temp = new Flower();

        for (int i = 1; i < length; i++) {
            for (int j = 0; j < length - i; j++) {
                if (data[j].getPrice() < data[j + 1].getPrice()) {
                    temp = data[j];
                    data[j] = data[j + 1];
                    data[j + 1] = temp;
                }
            }
        }
    }

    // 平均值、最大最小值方法
    public void average() {
        double sum = 0.0;
        double max = 0.0;
        double min = 100.0;
        for (int i = 0; i < length; i++) {
            sum += data[i].getPrice();
            // 三目运算法
            max = max > data[i].getPrice() ? max : data[i].getPrice();
            min = min < data[i].getPrice() ? min : data[i].getPrice();
        }
        System.out.printf("总价钱"+sum);
        System.out.printf("平均价钱为: %.2f\n", sum / length);
        System.out.println("最高价钱为: " + max + "\n" + "最低价钱为: " + min + "\n");
    }
    // 高于，低于平均值
    public void classify() {
        double sum = 0.0;
        int num1=0;
        int num2=0;
        for (int i = 0; i < length; i++) {
            sum += data[i].getPrice();
        }
        for(int m=0;m<length;m++) {
            if(data[m].getPrice()>=sum/length)
                num1++;
            else
                num2++;
        }
        System.out.println("高于平均值个数："+num1+"\n"+"低于平均值个数："+num2);
    }
    // 查询鲜花信息方法
    public void inquire() {
        System.out.print("请输入您要查询的花名：");
        String number2 = input.next();
        int i;
        for (i = 0; i < length; i++) {
            if (number2.equals(data[i].getName())) {
                System.out.println(data[i].getName() + "\t" +"\t"+ data[i].getPrice() + "\t"+"\t" +  data[i].getAmount());
                break;
            }
        }
        if (i == length) {
            System.out.println("查无此花请核对后重新输入 \n");
            inquire();
        }
    }



}